package com.wyx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyx.constans.Constans;
import com.wyx.domain.OperLog;
import com.wyx.dto.OperLogDto;
import com.wyx.mapper.OperLogMapper;
import com.wyx.service.OperLogService;
import com.wyx.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author xiaoxiong
 * @version 1.0
 * @date 2021/3/1 0:15
 */
@Service
public class OperLogServiceImpl implements OperLogService {
    @Autowired
    private OperLogMapper operLogMapper;


    @Override
    public void insertOperLog(OperLog operLog) {
        if (operLog != null) {
            this.operLogMapper.insert(operLog);
        }
    }

    @Override
    public DataGridView listForPage(OperLogDto operLogDto) {
        Page<OperLog> page = new Page<>(operLogDto.getPageNum(),operLogDto.getPageSize());
        QueryWrapper<OperLog> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNoneBlank(operLogDto.getTitle()),OperLog.COL_TITLE,operLogDto.getTitle());
        qw.eq(StringUtils.isNoneBlank(operLogDto.getStatus()), OperLog.COL_STATUS,operLogDto.getStatus());
        qw.like(StringUtils.isNoneBlank(operLogDto.getOperName()),OperLog.COL_OPER_NAME,operLogDto.getOperName());
        qw.eq(StringUtils.isNoneBlank(operLogDto.getBusinessType()),OperLog.COL_BUSINESS_TYPE,operLogDto.getBusinessType());
        qw.ge(operLogDto.getBeginTime() !=null,OperLog.COL_OPER_TIME,operLogDto.getBeginTime());
        qw.le(operLogDto.getEndTime()!=null,OperLog.COL_OPER_TIME,operLogDto.getEndTime());
        qw.orderByDesc(OperLog.COL_OPER_TIME);
        this.operLogMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public int deleteOperLogByIds(Long[] infoIds) {
        List<Long> longs = Arrays.asList(infoIds);
        if(longs!=null && longs.size()>0){
            return this.operLogMapper.deleteBatchIds(longs);
        }
        return 0;
    }

    @Override
    public int deleteAllOperLog() {
        // 删除所有的日志记录
        return this.operLogMapper.delete(null);
    }
}
